/*
 * Copyright (c) 2012 Google Inc.
 *
 * All rights reserved. This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at
 *
 * http://www.eclipse.org/legal/epl-v10.html
 */
package com.google.eclipse.xtext.filename.ui;

import static com.google.eclipse.xtext.filename.resource.MyDslContentHandler.MY_DSL_FILE_CONTENT_TYPE;
import static org.eclipse.emf.ecore.resource.ContentHandler.Registry.HIGH_PRIORITY;

import org.eclipse.emf.ecore.resource.ContentHandler;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.resource.IResourceServiceProvider;

import com.google.eclipse.xtext.filename.resource.MyDslContentHandler;
import com.google.inject.Provider;

/**
 * Utility methods for configuration of Xtext's behavior.
 *
 * @author alruiz@google.com (Alex Ruiz)
 */
final class XtextSetup {

  /**
   * Configures Xtext to associate the file name "MyDsl" to a Xtext editor. By default, Xtext
   * associates file extensions to a Xtext editor.
   * @param injectorProvider provides the {@code Injector} used by the "MyDsl" editor.
   */
  static void configureXtextToWorkWithFileNames(InjectorProvider injectorProvider) {
    register(new MyDslContentHandler());
    register(new ResourceFactoryDescriptor(injectorProvider));
    register(new ResourceServiceProvider(injectorProvider));
  }

  private static void register(ContentHandler h) {
    ContentHandler.Registry registry = ContentHandler.Registry.INSTANCE;
    registry.put(HIGH_PRIORITY, h);
  }

  private static void register(Resource.Factory.Descriptor d) {
    Resource.Factory.Registry registry = Resource.Factory.Registry.INSTANCE;
    registry.getContentTypeToFactoryMap().put(MY_DSL_FILE_CONTENT_TYPE, d);
  }

  private static void register(Provider<IResourceServiceProvider> p) {
    IResourceServiceProvider.Registry registry = IResourceServiceProvider.Registry.INSTANCE;
    registry.getContentTypeToFactoryMap().put(MY_DSL_FILE_CONTENT_TYPE, p);
  }

  private XtextSetup() {}
}
